package client.gui.roombox;

import info.clearthought.layout.TableLayout;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import listener.Listener;
import stalker.Stalker;
import broadcaster.Broadcaster;
import data.content.ContentSentence;
import data.visitor.ShowDataVisitor;

public class RoomBoxView extends JFrame implements Listener {
  
  private static final long serialVersionUID = 1L;
  
  private RoomController controller_;
  
  private long next_displayed_data_id_;
  
  private final JTextPane display_;
  
  private final JEditorPane user_list_;
  private final JPanel input_field_;
  
  private final JTextField input_text_;
  private final JTextField add_user_text_;
  
  private final JLabel add_user_label_;
  
  private String cached_display_;
  
  private void checkRep() {
    // TODO
  }
  
  public RoomBoxView(String name, RoomController controller) {
    super(name);
    
    assert controller != null && name != null;
    controller_ = controller;
    
    next_displayed_data_id_ = 0L;
    // setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    // set control pane
    Container cp = this.getContentPane();
    
    // sets the layout using table layout
    double[][] table_size = {
        {TableLayout.FILL, 400.0, 150.0},
        {TableLayout.FILL, 12.0, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, 12.0}
    };
    
    /*
     * +--------------------+
     * |MENUBAR             |
     * +----------+---------+
     * |TXT FIELD |USR FIELD|
     * |0, 0, 1, 2|2, 0     |
     * |          |         |
     * +-   -   - +---------+
     * |          |ADD LABEL|
     * |          |2, 1     |
     * +-   -   - +---------+
     * |          |ADD TXT  |
     * |          |2, 2     |
     * +----------+---------+
     * |INPUT TYPE TAB      |
     * |0, 3, 2, 3          |
     * +----------+---------+
     * |INPUT FIELD         |
     * |0, 4, 2, 4          |
     * +--------------------+
     * |LAST LOG            |
     * |0, 5, 2, 5          |
     * +--------------------+
     */
    
    cp.setLayout(new TableLayout(table_size));
    
    // text list
    display_ = new JTextPane();
    display_.setContentType("text/html");
    cached_display_ = "";
    display_.setText(cached_display_);
    JScrollPane display_pane_ = new JScrollPane(display_);
    display_pane_.setVerticalScrollBarPolicy(
                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    display_pane_.setPreferredSize(new Dimension(250, 250));
    display_.setEditable(false);
    
    cp.add(display_pane_, "0, 0, 1, 2");
    
    // user list
    user_list_ = new JEditorPane();
    user_list_.setEditable(false);
    cp.add(new JScrollPane(user_list_), "2, 0");
    
    // add user label
    add_user_label_ = new JLabel("Invite:");
    cp.add(add_user_label_, "2, 1");
    
    // add user text
    add_user_text_ = new JTextField();
    cp.add(add_user_text_, "2, 2");
    
    add_user_text_.addKeyListener(
      new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
          if (e.getKeyChar() == '\n') {
            if (add_user_text_.getText().length() > 0) {
              controller_.requestInviteUser(add_user_text_.getText());
            }
            add_user_text_.setText("");
          }
        }
      }
    );
    
    // tabs
    // TODO
    // cp.add(Tabs, "0, 3, 2, 3");
    
    // input field
    input_text_ = new JTextField();
    input_field_ = new JPanel();
    
    //table layout for input field
    double[][] input_table_size = {
        {TableLayout.FILL},
        {TableLayout.FILL}
    };
    
    input_field_.setLayout(new TableLayout(input_table_size));
    
    input_field_.add(input_text_, "0, 0");
    
    cp.add(input_field_, "0, 4, 2, 4");
    
    input_text_.addKeyListener(
      new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
          if (e.getKeyChar() == '\n') {
            controller_.requestWriteContent(
                new ContentSentence(input_text_.getText()));
            input_text_.setText("");
          }
        }
      }
    );
    
    // last log field
    // TODO
    
    // operation on close
    this.addWindowListener(new WindowAdapter() {
      
      public void windowClosing(WindowEvent arg0) {
        controller_.requestLeaveRoom();
        
      }
      // TODO
    });
    
    
    this.pack(); 
    
    checkRep();
  }
  
  public void appendText(String str) {
    cached_display_ = cached_display_ + str + "<br/>";
    display_.setText(cached_display_);
    display_.scrollRectToVisible(new Rectangle(0, display_.getHeight(), 1, 1));
  }

  public void respond(Broadcaster broadcaster) {
    assert broadcaster != null;
    if (!(broadcaster instanceof RoomBoxModel)) {
      assert false;
      return;
    }
    RoomBoxModel model = (RoomBoxModel) broadcaster;
    List<Stalker> on_users = model.getStalkers();
    String user_rep = "";
    for (Stalker stalker : on_users) {
      user_rep += stalker.getName() + '\n';
    }
    user_list_.setText(user_rep);
    while (next_displayed_data_id_ < model.dataSize()) {
      // TODO
      model.getDataAt(next_displayed_data_id_).accept(new ShowDataVisitor(this));
      ++next_displayed_data_id_;
    }
  }
  
}
